<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag type="list" v-model="model.line" dictCode="mes_line_cfg,NAME,LINE"
                                 placeholder="请选择产线或产线组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="op">
              <op-select placeholder="请选择工站" :line="model.line" :val="model.op" v-model="model.op" ></op-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="不良描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入不良描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="默认处理类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="Process_Type"
                                 placeholder="请选择默认处理类型" @change="typeChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="默认处理方案" :labelCol="labelCol"
                               :wrapperCol="wrapperCol" prop="defaultPlan">
              <a-select placeholder="请选择返工代码" v-model="model.defaultPlan">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option v-for="(item, key) in rwkOptions" :key="key" :value="item.code">
                  <span style="display: inline-block;width: 100%" :title="item.code">
                    {{ item.code }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="故障代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dc">
              <a-input v-model="model.dc" placeholder="请输入故障代码" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="顺序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">
              <a-input-number v-model="model.sequence" placeholder="请输入顺序号" style="width: 100%" />
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import OpSelect from '@comp/mes/OpSelect.vue'
import { validateDuplicateValue } from '@/utils/util'
import { duplicateCheck } from '@api/api'

export default {
  name: 'QisReinspectCfgForm',
  components: {
    OpSelect
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      isEdit: false,
      rwkOptions: [],
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        recId: [
          { required: true, message: '请输入主键!' }
        ],
        line: [
          { required: true, message: '请输入产线!' }
        ],
        description: [
          { required: true, message: '请输入不良描述!' }
        ],
        dc: [
          { required: true, message: '请输入故障代码!' },
          { validator: this.validateieNo }
        ]
      },
      url: {
        add: '/qis/qisReinspectCfg/add',
        edit: '/qis/qisReinspectCfg/edit',
        rwkList: '/qis/mesRwkCodeCfg/list',
        queryById: '/qis/qisReinspectCfg/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    validateieNo(rule, value, callback) {
      var params = {
        tableName: 'qis_reinspect_cfg',
        fieldName: 'DC',
        fieldVal: value,
        dbNo:'2',
        dataId: ''
      }
      if (this.isEdit){
        callback()
      }
      duplicateCheck(params).then((res) => {
        if (res.success) {
          console.log('通过！')
          callback()
        } else {
          callback('故障编码已存在!')
        }
      })
    },
    typeChange(val) {
      if (!this.model.op) {
        this.$message.warning('请先选择工站！')
        return
      }
      if (val) {
        this.getRwkList(val)
      }
    },
    getRwkList(typ) {
      this.model.code = ''
      let param = {
        line: this.model.line,
        op: this.model.op,
        pageNo: 1,
        pageSize: 100
      }
      if (typ) {
        param.type = typ
      }
      getAction(this.url.rwkList, param).then((res) => {
        this.rwkOptions = res.result.records
        if (this.rwkOptions.length === 1) {
          this.model.code = this.rwkOptions[0].code
        }
      })
    },
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>